/*
	This class used to management the texture resource.
	All the texture must be added in m_texture vector,
	and the Engine can use these texture by GetTexture function.
*/

#include "Texture.h"
#include "Error.h"

namespace BottomLayer
{
	extern LPDIRECT3DDEVICE9	g_pd3dDevice;
	extern Error g_err;

	Texture::Texture(void)
	{
		m_Texture.clear();
	}

	Texture::~Texture(void)
	{
	}

	HRESULT Texture::InitTexture()
	{
		return S_OK;
	}

	BOOL Texture::AddTexture(const char* name,const char* kind,int id,int state)
	{
		TEXTURE temp;
		char filename[256]="./Res/";
		strcat(filename,name);
		if( FAILED( D3DXCreateTextureFromFileEx( 
			g_pd3dDevice, 
			filename ,		
			0,
			0,
			0,
			0,
			D3DFMT_UNKNOWN, 
			D3DPOOL_DEFAULT,
			D3DX_DEFAULT,D3DX_DEFAULT, 
			D3DCOLOR_XRGB( 0, 0, 0 ),
			NULL,
			NULL, 
			&temp.m_texture ) ) )
		{
			//g_err.SendErrorMessage(ERR_FILE,filename);
			return FALSE;
		}
		temp.id=id;
		temp.station=0;
		temp.state=state;
		if(strcmp(kind,"Background")==0)
		{
			temp.kind=BACKGROUND;
		}
		else if(strcmp(kind,"Figure")==0)
		{
			temp.kind=FIGURE;
		}
		else if(strcmp(kind,"Systemres")==0)
		{
			temp.kind=SYSTEMRES;
		}
		else
		{
			//g_err.SendErrorMessage(ERR_XML,kind);
		}
		m_Texture.push_back(temp);
		return TRUE;
	}

	LPDIRECT3DTEXTURE9 Texture::GetTexture(TEXTURE_KIND kind,int id,int state)
	{
		vector<TEXTURE>::iterator iter;
		for(iter=m_Texture.begin();iter!=m_Texture.end();iter++)
		{
			if(iter->id==id&&iter->kind==kind&&iter->state==state)
			{
				return iter->m_texture;
			}
		}
		return NULL;
	}
}